using System.Web.Services;
using System.Xml;
using System;

namespace umlungu.engineX.blogX {
	/// <summary>
	/// umlungu EngineX.BlogX 
	/// This module is a component of the EngineX blogging and content management engine
	/// </summary>
	[WebService(Namespace="http://www.umlungu.com/engineX/")]
	public class blogX : x_result {
		#region Private methods
		x_blog blogx;
		x_user userx;
		#endregion

		#region Constructors/Destructors
		/// <summary>Constructor</summary>
		public blogX() : base("blogX") {
			userx = new x_user(Application);
			blogx = new x_blog(Application);
		}
		#endregion

		#region General web service methods
		/// <summary>Logs a user on to the system</summary>
		/// <remarks>NB: this is the only method that is not validated</remarks>
		/// <param name="username">login username</param>
		/// <param name="password">login password</param>
		[WebMethod] public XmlDocument Login(string username, string password) {
			try {
				userx.Login(username, password);
				_AddOk("token", userx.Token);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary>Tests</summary>
		/// <param name="token">the authentication token</param>
		[WebMethod] public XmlDocument Test(string token) {
			try {
				_AddOk();
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary>Validate user token against a particular right</summary>
		/// <param name="token">the authentication token</param>
		/// <param name="right">the right to validate</param>
		[WebMethod] public XmlDocument Validate(string token, string right) {
			try {
				userx.Validate(token, "Blog." + right);
				_AddOk();
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		#endregion

		#region Blog manage web service methods
		/// <summary></summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="topic">the name of the topic</param>
		/// <param name="archive">the name of the archive</param>
		/// <param name="id">the blog identifier</param>
		[WebMethod] public XmlDocument Get(string token, string name, string topic, string archive, int id) {
			try {
				userx.Validate(token, "Blog.Get");
				topic = (topic == "")? "default" : topic;
				if (archive == "" || archive == "current")
					blogx.Get(name, topic, id);
				else
					blogx.Get(name, topic, archive, id);
				_AddOk(blogx.Blog.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="topic">the name of the topic</param>
		/// <param name="archive">the name of the archive</param>
		/// <param name="ids">the blog identifiers</param>
		[WebMethod] public XmlDocument GetBlogs(string token, string name, string topic, string archive, string ids) {
			try {
				userx.Validate(token, "Blog.List");
				topic = (topic == "")? "default" : topic;
				if (archive == "" || archive == "current")
					blogx.Gets(name, topic, ids);
				else
					blogx.Gets(name, topic, archive, ids);
				_AddOk(blogx.Blogs.InnerXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="topic">the name of the topic</param>
		/// <param name="archive">the name of the archive</param>
		[WebMethod] public XmlDocument List(string token, string name, string topic, string archive) {
			try {
				userx.Validate(token, "Blog.List");
				topic = (topic == "")? "default" : topic;
				if (archive == "" || archive == "current")
					blogx.List(name, topic);
				else
					blogx.List(name, topic, archive);
				_AddOk(blogx.Blogs.InnerXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="topic">the name of the topic</param>
		/// <param name="title">the title of the blog</param>
		/// <param name="text">the text of the blog</param>
		[WebMethod] public XmlDocument AddText(string token, string name, string topic, string title, string text) {
			try {
				userx.Validate(token, "Blog.Add");
				topic = (topic == "")? "default" : topic;
				blogx.Add(name, topic, userx.Name, title, text, false);
				_AddOk(blogx.Blog.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="topic">the name of the topic</param>
		/// <param name="title">the title of the blog</param>
		/// <param name="html">the html of the blog</param>
		[WebMethod] public XmlDocument AddHtml(string token, string name, string topic, string title, string html) {
			try {
				userx.Validate(token, "Blog.Add");
				topic = (topic == "")? "default" : topic;
				blogx.Add(name, topic, userx.Name, title, html, true);
				_AddOk(blogx.Blog.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="topic">the name of the topic</param>
		/// <param name="title">the title of the blog</param>
		/// <param name="xml">the xml of the blog</param>
		[WebMethod] public XmlDocument Add(string token, string name, string topic, string title, string xml) {
			try {
				userx.Validate(token, "Blog.Add");
				topic = (topic == "")? "default" : topic;
				blogx.Add(name, topic, userx.Name, title, xml);
				_AddOk(blogx.Blog.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="topic">the name of the topic</param>
		/// <param name="id">the blog identifier</param>
		[WebMethod] public XmlDocument Delete(string token, string name, string topic, int id) {
			try {
				userx.Validate(token, "Blog.Delete");
				topic = (topic == "")? "default" : topic;
				blogx.Delete(name, topic, id);
				_AddOk();
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="topic">the name of the topic</param>
		/// <param name="id">the blog identifier</param>
		/// <param name="title">the title of the blog</param>
		/// <param name="xml">the xml of the blog</param>
		[WebMethod] public XmlDocument Edit(string token, string name, string topic, int id, string title, string xml) {
			try {
				userx.Validate(token, "Blog.Edit");
				topic = (topic == "")? "default" : topic;
				blogx.Update(name, topic, id, title, xml);
				_AddOk(blogx.Blog.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="topic">the name of the topic</param>
		/// <param name="id">the blog identifier</param>
		/// <param name="title">the title of the blog</param>
		/// <param name="text">the text of the blog</param>
		[WebMethod] public XmlDocument EditText(string token, string name, string topic, int id, string title, string text) {
			try {
				userx.Validate(token, "Blog.Edit");
				topic = (topic == "")? "default" : topic;
				blogx.Update(name, topic, id, title, text, false);
				_AddOk(blogx.Blog.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="topic">the name of the topic</param>
		/// <param name="id">the blog identifier</param>
		/// <param name="title">the title of the blog</param>
		/// <param name="html">the html of the blog</param>
		[WebMethod] public XmlDocument EditHtml(string token, string name, string topic, int id, string title, string html) {
			try {
				userx.Validate(token, "Blog.Edit");
				topic = (topic == "")? "default" : topic;
				blogx.Update(name, topic, id, title, html, true);
				_AddOk(blogx.Blog.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="topic">the name of the topic</param>
		/// <param name="id">the blog identifier</param>
		[WebMethod] public XmlDocument Comments(string token, string name, string topic, int id) {
			try {
				userx.Validate(token, "Comment.View");
				topic = (topic == "")? "default" : topic;
				blogx.Get(name, topic, id);
				XmlNode comments = blogx.Blog.SelectSingleNode("comments");
				if (comments != null)
					_AddOk(comments.OuterXml);
				else
					throw(new x_exception("error_nocomments", "no comments"));
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="topic">the name of the topic</param>
		/// <param name="id">the blog identifier</param>
		/// <param name="cid">the comment identifier</param>
		[WebMethod] public XmlDocument GetComment(string token, string name, string topic, int id, int cid) {
			try {
				userx.Validate(token, "Comment.View");
				topic = (topic == "")? "default" : topic;
				blogx.Get(name, topic, id);
				XmlNode comment = blogx.Blog.SelectSingleNode("comments/comment[@id='"+cid.ToString()+"']");
				if (comment != null)
					_AddOk(comment.OuterXml);
				else
					throw(new x_exception("error_commentexist", "comment does not exist"));
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="topic">the name of the topic</param>
		/// <param name="id">the blog identifier</param>
		/// <param name="author">the author of the comment</param>
		/// <param name="title">the title of the comment</param>
		/// <param name="text">the text of the comment</param>
		[WebMethod] public XmlDocument AddComment(string token, string name, string topic, int id, string author, string title, string text) {
			try {
				userx.Validate(token, "Comment.Add");
				if (author == "" || author == "null") {	// default to author from token user
					userx.Validate(token);	// since validation for 'public' rights don't load user
					author = userx.Name;
				}
				topic = (topic == "")? "default" : topic;
				blogx.AddComment(name, topic, id, userx.Username, author, title, text);
				_AddOk(blogx.Blog.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="topic">the name of the topic</param>
		/// <param name="id">the blog identifier</param>
		/// <param name="cid">the comment identifier</param>
		/// <param name="title">the title of the comment</param>
		/// <param name="text">the text of the comment</param>
		[WebMethod] public XmlDocument EditComment(string token, string name, string topic, int id, int cid, string title, string text) {
			try {
				userx.Validate(token, "Comment.Edit");
				topic = (topic == "")? "default" : topic;
				blogx.EditComment(name, topic, id, cid, title, text);
				_AddOk(blogx.Blog.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary>Delete the comment</summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="topic">the name of the topic</param>
		/// <param name="id">the blog identifier</param>
		/// <param name="cid">the comment identifier</param>
		[WebMethod] public XmlDocument DeleteComment(string token, string name, string topic, int id, int cid) {
			try {
				userx.Validate(token, "Comment.Delete");
				topic = (topic == "")? "default" : topic;
				blogx.DeleteComment(name, topic, id, cid);
				_AddOk(blogx.Blog.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary>Approve the comment</summary>
		/// <param name="token">the authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="topic">the name of the topic</param>
		/// <param name="id">the blog identifier</param>
		/// <param name="cid">the comment identifier</param>
		[WebMethod] public XmlDocument ApproveComment(string token, string name, string topic, int id, int cid) {
			try {
				userx.Validate(token, "Comment.Edit");
				topic = (topic == "")? "default" : topic;
				blogx.ApproveComment(name, topic, id, cid);
				_AddOk(blogx.Blog.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		
		#endregion

	}
}
